package bbs.controller.api;

import bbs.annotation.GlobalInterceptor;
import bbs.annotation.VerifyParam;
import bbs.controller.base.ABaseController;
import bbs.entity.config.WebConfig;
import bbs.entity.enums.ResponseCodeEnum;
import bbs.entity.vo.ResponseVO;
import bbs.exception.BusinessException;
import bbs.service.SysSettingService;
import bbs.utils.StringTools;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController("innerApiController")
@RequestMapping("/innerApi")
public class InnerApiController extends ABaseController {

    @Resource
    private WebConfig webConfig;

    @Resource
    private SysSettingService sysSettingService;

    @RequestMapping("/refresSysSetting")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO refresSysSetting(
            @VerifyParam(required = true) String appKey,
            @VerifyParam(required = true) Long timeStamp,
            @VerifyParam(required = true) String sign
    ) {
        if (!webConfig.getInnerApiAppKey().equals(appKey)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        if (System.currentTimeMillis() - timeStamp > 1000 * 10) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        String appSecret = webConfig.getInnerApiAppSecret();
        String mySign = StringTools.encodeMd5(appKey + timeStamp + appSecret);
        if (!mySign.equals(sign)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        sysSettingService.refresCache();
        return getSuccessResponseVO(null);
    }
}
